package gofish.json;

import gofish.objects.Player;

/**
 * JsonArray is a composite node in the AbstractJson family.
 * It holds an ArrayList (inherited from AbstractCompositeJson)
 * of AbstractJson elements, which for this class will all be
 * of type JsonArrayElement.
 * This inherits toString() from AbstractCompositeJson, which
 * will return "[" + toString() called on every element in the
 * ArrayList + "]".
 * @author Matt Lauber
 *
 */
public class JsonArray extends AbstractCompositeJson {

	/**
	 * This class has multiple constructors that all assign
	 * as their startSymbol and endSymbol (inherited) the symbols
	 * "[" and "]", as per JSON format.
	 * The no argument constructor only assigns those symbols. All
	 * other constructors do that and then add to their ArrayList whatever
	 * object you pass in as an argument. This way when you create a JsonArray,
	 * you can pre-populate it with a value by using the constructor.
	 * The values that can be passed to the constructor include String,
	 * an array of JsonArrayElement(s), an AbstractCompositeJson (which would
	 * either be a JsonObject or a JsonArray), a char, int, double, and boolean.
	 */
	public JsonArray() {
		super("[", "]");
	}
	
	public JsonArray(JsonArrayElement... elms) {
		super("[", "]");
		add(elms);
	}
	
	public JsonArray(AbstractCompositeJson element) {
		super("[", "]");
		add(element);
	}
	
	public JsonArray(String element) {
		super("[", "]");
		add(element);
	}
	
	public JsonArray(char element) {
		super("[", "]");
		add(element);
	}
	
	public JsonArray(int element) {
		super("[", "]");
		add(element);
	}
	
	public JsonArray(double element) {
		super("[", "]");
		add(element);
	}
	
	public JsonArray(boolean element) {
		super("[", "]");
		add(element);
	}
	
	/**
	 * The add() method adds whatever you pass in as an argument to this
	 * object's ArrayList, so that when you call toString(), it will be
	 * printed.  The add() method is overloaded so that it can accept
	 * the same arguments as the constructors.  If you use the addNull()
	 * method, which will simply set the value to a String representation
	 * of null. 
	 * @param element : the object to be added to the ArrayList
	 * @return : a reference to this object, so that add() calls can be strung together, as add(x).add(y).add(z) etc
	 */
	public JsonArray add(JsonArrayElement... elms) {
		for (JsonArrayElement e : elms) {
			elements.add(e);
		}
		return this;
	}
	
	public JsonArray add(AbstractCompositeJson element) {
		elements.add(element);
		return this;
	}
	
	public JsonArray add(String element) {
		elements.add( new JsonArrayElement(element) );
		return this;
	}
	
	public JsonArray add(char element) {
		elements.add( new JsonArrayElement(element) );
		return this;
	}
	
	public JsonArray add(int element) {
		elements.add( new JsonArrayElement(element) );
		return this;
	}
	
	public JsonArray add(double element) {
		elements.add( new JsonArrayElement(element) );
		return this;
	}
	
	public JsonArray add(boolean element) {
		elements.add( new JsonArrayElement(element) );
		return this;
	}
	
	public JsonArray addNull() {
		elements.add( new JsonArrayElement() );
		return this;
	}
	
	public JsonArray add(Player player) {
		JsonObject p = new JsonObject();
		//System.out.println(p);
		p.add("userName", player.getUserName());
		//System.out.println("username: " + player.getUserName()); //
		//System.out.println(p);
		// If she uses arrays instead of string, these may need to change:
		p.add("hand", player.getHand().length() / 2);
		//System.out.println("hand:" + player.getHand().length() / 2);
		//System.out.println(p);
		// may change if pairs is an array instead of a String
		// may need try-catch in for-loop
		JsonArray responsePairs = new JsonArray();
		String playersPairs = player.getPairs();
//		if (playersPairs.equals("")) {
//			System.out.println("here");
//			System.out.println(responsePairs);
//			System.out.println("here2");
//		}
//		else {
		//System.out.println(playersPairs.length());
		for (int i = 0; i < playersPairs.length(); ++i) {
			responsePairs.add( String.valueOf(playersPairs.charAt(i)) );
		}
//		}
		p.add("pairs", responsePairs);
		//System.out.println(p);
		
		add(p);
		
		return this;
	}
}
